import re

from sqlalchemy.orm import declarative_mixin, declared_attr


@declarative_mixin
class TableNameMixin:
    """Mixin for rewrite table name magic method."""

    pattern = re.compile(r"(?<!^)(?=[A-Z])")

    @declared_attr
    def __tablename__(cls) -> str:
        return cls.pattern.sub("_", cls.__name__).lower()
